package androidx.blogger.activities;

import com.google.gwt.user.client.rpc.AsyncCallback;

import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import androidx.blogger.BloggerApplication;
import androidx.blogger.Log;
import androidx.blogger.R;
import androidx.blogger.tasks.EditBlogEntryTask;
import androidx.blogger.tasks.PostBlogEntryTask;
import androidx.gdata.blogger.AppControl;
import androidx.gdata.blogger.AppDraft;
import androidx.gdata.blogger.Blog;
import androidx.gdata.blogger.BlogContent;
import androidx.gdata.blogger.BlogEntry;
import androidx.gdata.blogger.BlogEntryBuilder;

public class EditBlogPost extends ConnectedActivity {
  private TextView entryContentEditText;
  private TextView entryTitleEditText;
  private Button editButton;

  private static final int MENU_POST = Menu.FIRST;
  private static final int MENU_SAVE = Menu.FIRST + 1;
  private static final int MENU_CANCEL = Menu.FIRST + 2;

  @Override
  protected void onCreate2(Bundle savedInstanceState) {
    setContentView(R.layout.edit_post);
    entryContentEditText = (TextView) findViewById(R.id.contentEditText);
    entryTitleEditText = (TextView) findViewById(R.id.titleEditText);
    editButton = (Button) findViewById(R.id.postButton);
    BlogEntry editedPost = BloggerApplication.getInstance().getCurrentPost();
    if (editedPost == null)
      clear();
    else {
      entryContentEditText.setText(editedPost.getContent().getData());
      entryTitleEditText.setText(editedPost.getTitle());
    }
    editButton.setOnClickListener(postButtonClicked);
  }

  @Override
  protected void onStart() {
    super.onStart();
    Log.i("EditBlogPost.onStart()");
  }

  private void onPostBlog(boolean draft) {
    // Post the current blog post.
    String title = entryTitleEditText.getText().toString();
    String contentString = entryContentEditText.getText().toString();
    Blog target = BloggerApplication.getInstance().getCurrentBlog();
    BlogEntry editedPost = BloggerApplication.getInstance().getCurrentPost();

    BlogEntryBuilder beb = new BlogEntryBuilder();
    BlogEntry entry;
    if (editedPost == null) {
      entry =
          beb.setTitle(title).setBlogContent(new BlogContent("text", contentString)).setIsDraft(
              draft).build();
      PostBlogEntryTask.execute(getProgressIndicator("Posting to '" + target.getTitle() + "'"),
          target, entry, blogPostCallback);
    } else {
      entry = editedPost;
      entry.getContent().setData(contentString);
      AppDraft appDraft = new AppDraft(draft ? "yes" : "no");
      if (entry.getAppControl() == null) {
        entry.setAppControl(new AppControl(appDraft));
      } else {
        entry.getAppControl().setDraft(appDraft);
      }
      entry.setTitle(title);
      // entry = beb.setTitle(title).setBlogContent(new BlogContent("text",
      // contentString)).setIsDraft(draft).build();
      EditBlogEntryTask.execute(getProgressIndicator("Posting to '" + target.getTitle() + "'"),
          target, entry, blogPostCallback);
    }
  }

  private AsyncCallback<Blog> blogPostCallback = new AsyncCallback<Blog>() {
    public void onSuccess(Blog result) {
      onReceiveBlogPostResult(result);
    }

    public void onFailure(Throwable t) {
      longMessage("Problem posting blog entry!");
    }
  };

  private void onReceiveBlogPostResult(Blog blog) {
    Log.i("Posted blog entry!");
    longMessage("Post successful!");
    finish();
  }

  private void clear() {
    entryTitleEditText.setText("");
    entryContentEditText.setText("");
  }

  OnClickListener postButtonClicked = new OnClickListener() {
    public void onClick(View arg0) {
      onPostBlog(true);
    }
  };

  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
    super.onCreateOptionsMenu(menu);
    menu.add(Menu.NONE, MENU_POST, Menu.NONE, "Post").setIcon(android.R.drawable.ic_menu_add);
    menu.add(Menu.NONE, MENU_SAVE, Menu.NONE, "Save as Draft").setIcon(
        android.R.drawable.ic_menu_save);
    menu.add(Menu.NONE, MENU_CANCEL, Menu.NONE, "Cancel").setIcon(
        android.R.drawable.ic_menu_close_clear_cancel);
    return true;
  }

  @Override
  public boolean onPrepareOptionsMenu(Menu menu) {
    super.onPrepareOptionsMenu(menu);
    menu.findItem(MENU_CANCEL).setTitle(
        BloggerApplication.getInstance().getCurrentPost() == null ? "Discard" : "Cancel");
    return true;
  }

  /** Process the options menu item selection */
  @Override
  public boolean onOptionsItemSelected(MenuItem item) {
    super.onOptionsItemSelected(item);
    switch (item.getItemId()) {
      case MENU_POST:
        onPostBlog(false);
        return true;
      case MENU_SAVE:
        onPostBlog(true);
        return true;
      case MENU_CANCEL:
        clear();
        finish();
        return true;
    }
    return false;
  }

}
